//*************************************************************************//
//*           _____   ____________            ______               __     *//
//*          / /__ \ / ____/ ____/           / ____/___  _________/ /     *//
//*     __  / /__/ // __/ / __/    ______   / /_  / __ \/ ___/ __  /      *//
//*    / /_/ // __// /___/ /___   /_____/  / __/ / /_/ / /  / /_/ /       *//
//*    \____//____/_____/_____/           /_/    \____/_/   \__,_/        *//
//*                                                                       *//
//*     Copyright (c) 2006 Ford Argentina. All Rights Reserved.           *//
//*                                                                       *//
//*************************************************************************//
package ar.com.ford.it.webengine.helper;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import ar.com.ford.it.webengine.settings.AppSettings;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.Level;
import com.ford.it.logging.LogFactory;

/**
 * JavaPackage <br>
 * @author Aji
 */
public class JavaPackage {
	private static final String CLASS_NAME = JavaPackage.class.getName();
	private static final ILogger log = LogFactory.getInstance().getLogger(CLASS_NAME);

	private ArrayList childrens;
	private HashMap classes;
	private String name;
	
    /**
     * Constructor
     * @param name Nombre del package
     * @param recursive true para cargar recursivamente los packages internos
     */
	public JavaPackage(String name, boolean recursive) {
		String METHOD_NAME = "JavaPackage";
		
		log.entering(CLASS_NAME, METHOD_NAME);
		
		this.name = name;
		this.classes = new HashMap();
		
		log.logp(Level.FINE, CLASS_NAME, METHOD_NAME, "name", name);
				
		String url = name.replace('.', File.separatorChar);
		String directory = AppSettings.getInstance().getAPP_PATH();
		directory += File.separator+"WEB-INF"+File.separator+"classes"+File.separator+url;

		log.logp(Level.FINE, CLASS_NAME, METHOD_NAME, "directory", directory);
		
		File dir = new File(directory);
		FileFilter filter = new FileFilter() { 
									public boolean accept(File file) {
										String name = file.getName();
										return file.isFile() && name.endsWith(".class");
									}
								};
		
		File[] files = dir.listFiles(filter);

		try {
		
			for (int i=0; i<files.length; i++) {
				int tam = files[i].getName().length()-6;
				String className = files[i].getName().substring(0, tam);
				log.logp(Level.FINE, CLASS_NAME, METHOD_NAME, "className", className);
				classes.put(className, Class.forName(this.name+"."+className));
			}
			
		}
		catch (Exception ex) {
			
		}
		

		log.logp(Level.FINE, CLASS_NAME, METHOD_NAME, "recursive", Boolean.toString(recursive));	
		if (recursive) {
			FileFilter dirFilter = new FileFilter() { 
										public boolean accept(File file) {
											return file.isDirectory();
										}
									};
			childrens = new ArrayList();
					
			File[] dirs = dir.listFiles(dirFilter);
			for (int j=0; j<dirs.length; j++) {
				log.logp(Level.FINE, CLASS_NAME, METHOD_NAME, "package", dirs[j].getName());				
				childrens.add(new JavaPackage(this.name+"."+dirs[j].getName(), true));
			}
		}
		
		log.exiting(CLASS_NAME, METHOD_NAME);
	}


	/**
	 * Returns the childrens.
	 * @return ArrayList
	 */
	public ArrayList getChildrens() {
		return childrens;
	}

	/**
	 * Returns the classes.
	 * @return ArrayList
	 */
	public HashMap getClasses() {
		return classes;
	}


	/**
	 * Method getClassesCollection.
	 * @return Collection
	 */
	public Collection getClassesCollection() {
		if (classes != null)
			return classes.keySet();
		return null;
	}


	/**
	 * Returns the name.
	 * @return String
	 */
	public String getName() {
		return name;
	}

	/**
	 * Method getInnerClass.
	 * @param className
	 * @return Class
	 */
	public Class getInnerClass(String className) {
		return (Class)classes.get(className);
	}
	
	

}
